package com.tubi.edu.info.manager.dao;

import com.tubi.edu.info.manager.domain.Teacher;

public class TeacherDao {
    public static Teacher[] teachers = new Teacher[5];

    //添加老师
    public boolean addTeacher(Teacher t) {
        int index = -1;
        //2。2 遍历数组取出每一个元素是否为null
        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i] == null){
                index = i;
                //2。3如果是null 让index 遍历记录当前索引位置，使用break结束遍历
                break;
            }
        }
        if(index == -1) {
            return false;
        }else {
            teachers[index] = t;
            return true;
        }
    }
    //返回所有老师
    public Teacher[] findAllTeacher() {
        return  teachers;
    }

    //删除
    public void deleteStudentById(String id) {
        int index = getIndex(id);
        teachers[index] = null;
    }
    //查询老师的索引
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if(teacher != null && id.equals(teacher.getId())) {
                index = i;
                break;
            }
        }
        return index;
    }
    //修改老师
    public void updateTeacher(String id, Teacher t) {
        int index = getIndex(id);
        teachers[index] = t;
    }
}
